﻿using L.EntityFramework;
using NB.MGJ.FrameWork;
using NB.MGJ.Model.AntiFake;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NB.MGJ.Service.AntiFake
{
    public  class AntiFakeButtonService :BaseService
    {
        public BaseRepository _baseRepository = new BaseRepository();
        private BaseRepository<L.EntityModel.EntityContext> baserReposityory2;
        public AntiFakeButtonService()
        {
            baserReposityory2 = CreateBaseRepository<L.EntityModel.EntityContext>();
        }

        #region  增
        public int Add(L.EntityModel.Antifake_Button model)
        {
            try
            {
                baserReposityory2.Insert<L.EntityModel.Antifake_Button>(model);
                return model.ButtonId;
                //int result = _baseRepository.InsertReturnID<antifake_button>(model);
                //return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
          
        }
        #endregion

        #region 删
        public int Del(int id)
        {
            string sql = "select * from antifake_button where ButtonId=" + id;
            var model = _baseRepository.ExecuteSqlCollection<antifake_button>(sql).ToList().FirstOrDefault();
            model.ViewDel = 1;
            int result = _baseRepository.Update<antifake_button>(model);
            return result;
        }
        #endregion

        #region 改
        public int Update(L.EntityModel.Antifake_Button model)
        {
            return baserReposityory2.Update<L.EntityModel.Antifake_Button>(model);

            #region MyRegion
            //int result = _baseRepository.Update<antifake_button>(model);
            //return result; 
            #endregion
        }
        #endregion

        #region 查
        public L.EntityModel.Antifake_Button GetModel(int id)
        {
            return baserReposityory2.SearchFirstOrDefault<L.EntityModel.Antifake_Button>(p => p.ButtonId == id);

            #region MyRegion
            //string sql = "select * from antifake_button where ButtonId=" + id;
            //var model = _baseRepository.ExecuteSqlCollection<antifake_button>(sql).ToList().FirstOrDefault();
            //return model; 
            #endregion
        }


        public List<antifake_button> GetListByModuleId(int moduleId)
        {
            string sql = "select * from antifake_button where ModuleId=" + moduleId + " and ViewDel=0";

            var list = _baseRepository.ExecuteSqlCollection<antifake_button>(sql).ToList();

            return list;
        }

        /// <summary>
        /// 用于企业尚未创建页面配置的场景
        /// </summary>
        /// <returns></returns>
        public List<antifake_button> CreateButtonListTemplet(int TID)
        {


            if (TID == 0 || TID == 1)
            {
                antifake_button btn1 = new antifake_button();
                btn1.Link = 0;
                btn1.Name = "防伪";
                btn1.ViewDel = 0;
                btn1.BindValue = "0";
                btn1.ModuleId = 0;
                btn1.IcoUrl = "/Content/CodeSearchImg/bql1.png";


                antifake_button btn2 = new antifake_button();
                btn2.Link = 1;
                btn2.Name = "溯源";
                btn2.ViewDel = 0;
                btn2.BindValue = "0";
                btn2.ModuleId = 0;
                btn2.IcoUrl = "/Content/CodeSearchImg/bql2.png";

                antifake_button btn3 = new antifake_button();
                btn3.Link = 2;
                btn3.Name = "网上订购";
                btn3.ViewDel = 0;
                btn3.BindValue = "www.taobao.com";
                btn3.ModuleId = 0;
                btn3.IcoUrl = "/Content/CodeSearchImg/bql3.png";

                antifake_button btn4 = new antifake_button();
                btn4.Link = 2;
                btn4.Name = "企业官网";
                btn4.ViewDel = 0;
                btn4.BindValue = "www.baidu.com";
                btn4.ModuleId = 0;
                btn4.IcoUrl = "/Content/CodeSearchImg/bql4.png";


                List<antifake_button> list = new List<antifake_button>();
                list.Add(btn1);
                list.Add(btn2);
                list.Add(btn3);
                list.Add(btn4);
                return list;
            }
            else if (TID == 2)
            {





                antifake_button btn1 = new antifake_button();
                btn1.Link = 0;
                btn1.Name = "防伪";
                btn1.ViewDel = 0;
                btn1.BindValue = "0";
                btn1.ModuleId = 0;
                btn1.IcoUrl = "http://avrilbucket.oss-cn-beijing.aliyuncs.com/44b9aa62-7e07-4149-8ccc-90e0bf51feb1icon1.png";


                antifake_button btn2 = new antifake_button();
                btn2.Link = 1;
                btn2.Name = "溯源";
                btn2.ViewDel = 0;
                btn2.BindValue = "0";
                btn2.ModuleId = 0;
                btn2.IcoUrl = "http://avrilbucket.oss-cn-beijing.aliyuncs.com/03a479b2-072c-4647-8b61-f5feb7394bd6icon2.png";

                antifake_button btn3 = new antifake_button();
                btn3.Link = 2;
                btn3.Name = "网上订购";
                btn3.ViewDel = 0;
                btn3.BindValue = "www.taobao.com";
                btn3.ModuleId = 0;
                btn3.IcoUrl = "http://avrilbucket.oss-cn-beijing.aliyuncs.com/4606dd5d-c3d6-4c0c-882e-b176da6e7be6icon3.png";

                antifake_button btn4 = new antifake_button();
                btn4.Link = 2;
                btn4.Name = "企业官网";
                btn4.ViewDel = 0;
                btn4.BindValue = "www.baidu.com";
                btn4.ModuleId = 0;
                btn4.IcoUrl = "http://avrilbucket.oss-cn-beijing.aliyuncs.com/68ed54e1-268b-41c4-999e-404987b87d1eicon4.png";


                List<antifake_button> list = new List<antifake_button>();
                list.Add(btn1);
                list.Add(btn2);
                list.Add(btn3);
                list.Add(btn4);
                return list;

            }
            return null;

        }

        public bool BindButtonListModuleId (List<antifake_button> list,int moduleId)
        {
            try
            {
                foreach (var item in list)
                {
                    item.ModuleId = moduleId;
                    _baseRepository.InsertReturnID<antifake_button>(item);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
                throw ex;
            }

        }






        public bool CreateButtonListTempletAndBindPage(int moduleId,int TID)
        {
            var list = CreateButtonListTemplet(TID);
            var result= BindButtonListModuleId(list,moduleId);
            return result;
        }


        #endregion

    }
}
